# Copyright 2012 Brett Ponsler
# This file is part of siriviewer.
#
# siriviewer is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# siriviewer is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with siriviewer.  If not, see <http://www.gnu.org/licenses/>.
''' '''
from os.path import join

from PyQt4.Qt import SIGNAL
from PyQt4 import uic, QtGui, Qt, QtCore

from siriviewer.objectView import ObjectView
from siriviewer.siriButton import SiriButtonWidget

from pysiriproxy.constants import Directions

from pyamp.ui.qt import BaseWidget, StyleSheet


class SiriView(BaseWidget):
    ''' '''

    def createElements(self):
        ''' '''
        self.__objectView = ObjectView("ObjectView", self.controller,
                                       self.mainWindow)

        self.__siriButton = SiriButtonWidget()
        self.__speechText = QtGui.QLineEdit()

        return [self.__objectView, self.__siriButton, self.__speechText]

    def setupConnections(self):
        ''' '''
        # Connect the return key, and the button press to the search method
        self.connect(self.__speechText, SIGNAL("returnPressed()"),
                     self.__onSpeechRequest)
        self.connect(self.__siriButton, SIGNAL("clicked()"),
                     self.__onSpeechRequest)

    def show(self):
        ''' '''
        QtGui.QWidget.show(self)
        self.__speechText.setFocus()
    
    def addRequestObject(self, request):
        ''' '''
        self.__objectView.addRequestObject(request)

    def addResponseObject(self, obj):
        ''' '''
        self.__objectView.addResponseObject(obj)

    def __onSpeechRequest(self):
        '''Called in the event that the currently typed speech request should
        be executed.

        '''
        # Grab the text the user wishes to speak
        speech = str(self.__speechText.text()).strip()

        # Ensure that the speech exists before creating it
        if speech is not None and len(speech) > 0:
            # Add the request object to the view
            self.addRequestObject(speech)

            # Now test the speech request
            self.controller.testSpeech(speech)

            # Clear the text in the text box when a request is processed
            self.__speechText.setText("")
